package setting

import (
	"context"
	"strconv"
	"strings"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/blog/util"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/miniapp"
	"mall/service/blog/rpc/blogclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetSettingPostListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetSettingPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSettingPostListLogic {
	return &GetSettingPostListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetSettingPostListLogic) GetSettingPostList(req *types.SectionKBPostListReq) (resp *types.SectionKBPostListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorterNew(req.Sorter)
	filterBys := util.ParseFilterNew(req.Filter, req.Type)

	var ids *string
	var search *string
	if req.Search != nil && len(strings.TrimSpace(*req.Search)) > 0 { // 前端可以搜标题或者ID
		reqSearch := strings.TrimSpace(*req.Search)
		if _, err := strconv.ParseInt(reqSearch, 10, 64); err == nil {
			ids = pointy.GetPointer(reqSearch)
		} else {
			search = pointy.GetPointer(reqSearch)
		}
	}

	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}
	if req.PostIds != nil && len(strings.TrimSpace(*req.PostIds)) > 0 {
		ids = pointy.GetPointer(strings.TrimSpace(*req.PostIds))
	}

	data, err := l.svcCtx.BlogRpc.BackendGetPostList(l.ctx, &blogclient.TmpPostListReq{
		Page:      page,
		PageSize:  pageSize,
		Ids:       ids,
		Title:     req.Title,
		Slug:      req.Slug,
		Search:    search,
		Status:    req.Status,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		OrderBy:   orderBys,
		FilterBy:  filterBys,
		WithMeta:  req.WithMeta,
		WithMedia: req.WithMedia,
	})
	if err != nil {
		return nil, err
	}

	var withLink bool
	if req.WithLink != nil && *req.WithLink > 0 {
		withLink = true
	}

	resp = &types.SectionKBPostListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.SectionKBPostListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}
	for _, v := range data.List {
		var media []*types.MediaInfo
		if len(v.Media) > 0 {
			for _, m := range v.Media {
				var url *string
				if m.Url != nil {
					url = m.Url
				} else if m.Uri != nil {
					url = m.Uri
				}
				media = append(media, &types.MediaInfo{
					Uid:       m.Uid,
					Id:        m.Id,
					Thumbnail: m.Thumbnail,
					Order:     m.Order,
					Url:       url,
					Name:      m.Name,
					MimeType:  m.MimeType,
					AltText:   m.AltText,
					MetaData:  m.MetaData,
				})
			}
		}

		var link *string
		if withLink {
			link = pointy.GetPointer(miniapp.GetPostLinkUrl(v.Id))
		}

		resp.Data.List = append(resp.Data.List, &types.SectionKBPostInfo{
			Id:     v.Id,
			Title:  v.Title,
			Slug:   v.Slug,
			Sort:   v.Sort,
			Status: v.Status,
			Link:   link,
			Media:  media,
		})
	}

	return resp, nil
}
